<template>
  <Dialog v-model="visiable" :title="formTitle" :width="700" @submit="onSubmit">
    <el-form :model="formData" :rules="formRules" ref="formRef" label-width="auto" label-suffix="：">
      <el-form-item label="编号" prop="bh">
        <el-input v-model="formData.bh" placeholder="请输入编号" />
      </el-form-item>
      <el-form-item label="名称" prop="mc">
        <el-input v-model="formData.mc" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="状态" prop="zt">
        <el-radio-group v-model="formData.zt">
          <el-radio :value="SECURITY_STATUS['启用']">启用</el-radio>
          <el-radio :value="SECURITY_STATUS['禁用']">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="name == '点标记'" label="图标" prop="icon">
        <el-input v-model="formData.icon" placeholder="请选择图标" @focus="onIconInputFocus" />
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script setup>
import { computed, ref } from 'vue';
import { SECURITY_STATUS } from '@/views/SystemSetting/config'

const emits = defineEmits(['submited']);
const props = defineProps({
  name: {
    type: String,
    default: '',
  },
});

const visiable = ref(false);
const formType = ref('add'); // add, edit
const formTitle = computed(() => `${formType.value === 'add' ? '新增' : '编辑'}${props.name}`);

const formData = ref({});
const formRules = {
  bh: [{ required: true, message: '请输入编号', trigger: 'blur' }],
  mc: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  zt: [{ required: true, message: '请选择状态', trigger: 'blur' }],
}

const initFormData = (type, row) => {
  if (type == 'add') formData.value = { zt: SECURITY_STATUS['启用'] };
  if (type == 'edit') formData.value = { ...row };
}

const show = (type = 'add', row = null) => {
  formType.value = type;
  initFormData(type, row);
  visiable.value = true;
};

const onSubmit = () => {
  setTimeout(() => {
    visiable.value = false;
    emits('submited')
  }, 2000);
}

const onIconInputFocus = () => {
  console.log('onIconInputFocus');
  
}

defineExpose({ show });
</script>

<style lang="scss" scoped>

</style>